import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import '../../../common/constant/colors.dart';
import '../../../common/constant/icon.dart';
import '../../../common/constant/styles.dart';
import 'package:app_core/app_core.dart';
import '../../../common/intl/index.dart';
import 'forgot_pwd_logic.dart';

class ForgotPwdPage extends StatelessWidget {
  ForgotPwdPage({super.key});

  final logic = Get.put(ForgotPwdLogic());

  @override
  Widget build(BuildContext context) {
    return DismissKeyboardWidget(
      child: Scaffold(
        // appBar: APPBar.textBar("忘记密码"),
        body: SingleChildScrollView(
          child: ConstrainedBox(
            constraints: BoxConstraints(
              minHeight: 1000,
            ),
            child: IntrinsicHeight(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SizedBox(height: 24),
                  _buildEmailField(),
                  SizedBox(height: 24),
                  _buildPasswordField(),
                  SizedBox(height: 24),
                  _buildConfirmPasswordField(),
                  Expanded(flex: 2, child: SizedBox.expand()),
                  _buildLoginButton(),
                  Expanded(flex: 1, child: SizedBox.expand()),
                  // SizedBox(height: AppScreenInfo.safeAreaBottom.value),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  // 邮箱输入框
  Widget _buildEmailField() {
    return GetBuilder<ForgotPwdLogic>(
      id: ForgotPwdUpdateKey.email,
      builder: (logic) {
        return Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 16),
              child: AppTextField.titleInput(
                focusNode: logic.emailFocusNode,
                controller: logic.emailController,
                title: LoginIntl.email.tr,
                hintText: LoginIntl.enterEmail.tr,
                keyboardType: TextInputType.emailAddress,
                textInputAction: TextInputAction.next,
                onEditingComplete: () {
                  logic.validateEmail();
                },
                onSubmitted: (p0) {
                  logic.passwordFocusNode.requestFocus();
                },
                suffixIcons:
                    logic.emailController.text.isNotEmpty &&
                            logic.emailFocusNode.hasFocus
                        ? [
                          AppGesture(
                            onTap: () {
                              logic.emailController.clear();
                            },
                            child: Image.asset(AppIcon.icClose),
                          ),
                        ]
                        : [],
              ),
            ),
            // if (!logic.isEmailValid) _buildError(LoginIntl.enterValidEmail.tr),
          ],
        );
      },
    );
  }

  // 密码输入框
  Widget _buildPasswordField() {
    return GetBuilder<ForgotPwdLogic>(
      id: ForgotPwdUpdateKey.password,
      builder: (logic) {
        return Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 16),
              child: AppTextField.titleInput(
                focusNode: logic.passwordFocusNode,
                controller: logic.passwordController,
                title: LoginIntl.password.tr,
                hintText: LoginIntl.enterPassword.tr,
                obscureText: !logic.showPassword,
                textInputAction: TextInputAction.next,
                onEditingComplete: () {
                  logic.validatePassword();
                },
                onSubmitted: (p0) {
                  logic.confirmPasswordFocusNode.requestFocus();
                },
                suffixIcons: [
                  if (logic.passwordController.text.isNotEmpty &&
                      logic.passwordFocusNode.hasFocus)
                    AppGesture(
                      onTap: () {
                        logic.passwordController.clear();
                      },
                      child: Image.asset(AppIcon.icClose),
                    ),
                  AppGesture(
                    onTap: logic.togglePasswordVisibility,
                    child: SvgPicture.asset(
                      logic.showPassword
                          ? AppIcon.icPwdShow
                          : AppIcon.icPwdHide,
                    ),
                  ),
                ],
              ),
            ),
          ],
        );
      },
    );
  }

  // 确认密码输入框
  Widget _buildConfirmPasswordField() {
    return GetBuilder<ForgotPwdLogic>(
      id: ForgotPwdUpdateKey.confirmPassword,
      builder: (logic) {
        return Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 16),
              child: AppTextField.titleInput(
                focusNode: logic.confirmPasswordFocusNode,
                controller: logic.confirmPasswordController,
                title: LoginIntl.confirmPassword.tr,
                obscureText: !logic.showConfirmPassword,
                hintText: LoginIntl.enterConfirmPassword.tr,
                onEditingComplete: () {
                  logic.validateConfirmPassword();
                },
                onSubmitted: (p0) {
                  logic.confirmPasswordFocusNode.unfocus();
                },
                suffixIcons: [
                  if (logic.confirmPasswordController.text.isNotEmpty &&
                      logic.confirmPasswordFocusNode.hasFocus)
                    AppGesture(
                      onTap: () {
                        logic.confirmPasswordController.clear();
                      },
                      child: Image.asset(AppIcon.icClose),
                    ),
                  AppGesture(
                    onTap: logic.toggleConfirmPasswordVisibility,
                    child: SvgPicture.asset(
                      logic.showConfirmPassword
                          ? AppIcon.icPwdShow
                          : AppIcon.icPwdHide,
                    ),
                  ),
                ],
              ),
            ),
            if (!logic.isPasswordValid || !logic.isConfirmPasswordValid)
              _buildError(
                !logic.isPasswordValid
                    ? LoginIntl.passwordRequirement.tr
                    : LoginIntl.passwordsDoNotMatch.tr,
              ),
          ],
        );
      },
    );
  }

  // 登录按钮
  Widget _buildLoginButton() {
    return GetBuilder<ForgotPwdLogic>(
      id: ForgotPwdUpdateKey.button,
      builder: (controller) {
        return Padding(
          padding: EdgeInsets.symmetric(horizontal: 16),
          child: AppButton.text(
            text: LoginIntl.confirm.tr,
            onTap: controller.sendCode,
            enable: controller.canSubmit,
            disableBackgroundColor: AppColor.buttonDisabled,
          ),
        );
      },
    );
  }

  // 验证错误提示
  Widget _buildError(String errorText) {
    return Padding(
      padding: EdgeInsets.only(left: 20, right: 20, top: 8),
      child: Text(
        errorText,
        style: AppStyles.s12.copyWith(color: AppColor.textError),
      ),
    );
  }
}
